var fs = require('fs');
//var colors = require('colors');

//usage:more filename
function more(filename){
	fs.readFile(__dirname+'/'+filename,'utf-8',function(err,data){
		if (err) {
			console.log(err);
		};
		console.log(data);
	});
}

function ls () {
	// list current dir files [usage:ls]
	fs.readdir(process.cwd(),function(err,data){
		if(err){
			console.log(err);
			return;			
		}
		for (var i = data.length - 1; i >= 0; i--) {
			console.log(data[i]);
		};
	});
}

//usage:cp src disc
function cp(src,disc){
	var reader = fs.createReadStream(src);
	var writer = fs.createWriteStream(disc);
	reader.on('data',function(chunk){
		writer.write(chunk);
	});
	reader.on('end',function(){
		writer.end();
	})
	writer.on('error',function(err){
		console.log("Err:",err)
	})
	reader.on('error',function(err){
		console.log('Err',err)
	})
}


function shell(){
	var cmds = process.argv.splice(2,process.argv.length);
	var cmd = cmds[0]; 
	if(cmd == 'ls')
		ls();
	else if(cmd == 'more')
		more(cmds[1]);
	else if(cmd == 'cp')
		cp(cmds[1],cmds[2])
}

//shell();

var confString = {
	os:'ubuntu',
	kernel:'linux3',
	user:'smark'
}

var data = JSON.stringify(confString);

fs.writeFile('conf.json',data,function(err){
	if(err)
		console.log(err);
	else 
		console.log("saved ok...")

});

